/*****************************************************************************
 *
 * Copyright (c), 2004-2020,ShangHai Neotune Information Technology Co., Ltd.
 *
 * Module Name : vbdefs.h
 *
 * Author :    sean
 *
 * Date :    2004-02-05
 *
 * Version :    1.00
 *
 * Description :    
 *
 * Other :    
 *
 * Data Structure List: <Main Data Structure>
 *
 * Modification History
 * ---------------------------------------------------------------------------
 *  1: Data :      2004-02-05
 *    Author :     sean
 *    Version :    V100R001M00
 *    Modification : creation
 *
*****************************************************************************/


#if (!defined(vb_inc))
#define vb_inc

#ifdef __cplusplus
extern"C" {
#endif

#if (!defined(asn1_inc))
#include <asn1.h>
#endif

#if (!defined(buffer_inc))
#include <buffer_snmp.h>
#endif

#if (!defined(mib_inc))
#include <mib.h>
#endif

#if (!defined(ui64_inc))
#include <ui64.h>
#endif

#include "buffer_snmp.h"

/* Definition for routine to free private vb space */
typedef void VBPRIVPROC_T __((PTR_T));

/* Definition for routine to undo sets */
struct VB_S;
typedef void UNDOPROC_T __((OIDC_T lastmatch, int tcount, OIDC_T *tlist, 
                            struct SNMP_PKT_S *pktp, struct VB_S *vbp));

#ifdef NEOSNET_STRUCT_ALIGN
#pragma align 1
#endif

/* Define the internal shape of a VarBind       */
typedef struct VB_S
{
    struct VB_S *   vb_link;          /* For the user to use, usually   */
                                      /*  to build a list of related    */
                                      /*  VB_Ts.                        */
    ALENGTH_T       vb_seq_size;      /* Overall length of the VarBind  */
                                      /* sequence when encoded.         */
    OBJ_ID_T        vb_obj_id;        /* Object id for this VarBind     */
    OCTET_T         vb_data_flags_n_type;  /* Class form & type of data */
    OCTET_T         vb_flags;         /* See below                      */
    ALENGTH_T       vb_data_length;   /* Length of the data when encode */
    MIBLOC_T        vb_ml;            /* The mib-leaf associated with   */
                                      /* this VarBind.                  */
    union   {
            INT_32_T        v_number;       /* Integer kinds of things  */
            UINT_32_T       v_counter;      /* Unsigned int things      */
            EBUFFER_T       v_string;       /* Octetstring things       */
            OBJ_ID_T        v_object;       /* Object id things         */
            unsigned char   v_network_address[4];
            UINT_64_T       v_counter64;    /* 64 bit unsigned things   */
            } value_u;

    union   {
            ptr_t    p_ptr;           /* storage space for moving info  */
            bits32_t p_int;           /* between test and set routines  */
            } priv;

    VBPRIVPROC_T *  vb_free_priv;     /* pointer to routine to free the */
                                      /* private storage space          */
    UNDOPROC_T   *  undoproc;         /* pointer to routine to undo sets*/
#if (INSTALL_ENVOY_EPI_SUBAGENT_SCHEME)
    UINT_32_T       sa_flags;
    OCTET_T         vb_access;
#endif
#if (INSTALL_ENVOY_AGENTX)
    OBJ_ID_T        ax_search_end;
    OCTET_T         ax_flags;
#endif
} VB_T;

#define v_gauge         v_counter
#define v_timeticks     v_counter
#define v_uinteger32    v_counter
#define v_opaque        v_string

#define vb_priv         priv.p_ptr
#define vb_int          priv.p_int

/* Values for vb_data_flags_n_type      */
#define VT_NUMBER       A_INTEGER
#define VT_STRING       A_OCTETSTRING
#define VT_BITS         A_OCTETSTRING           /* same as a string */
#define VT_OBJECT       A_OBJECTID
#define VT_EMPTY        A_NULL
#define VT_IPADDRESS    (A_APPLICATION | 0)
#define VT_COUNTER      (A_APPLICATION | 1)
#define VT_GAUGE        (A_APPLICATION | 2)
#define VT_UNSIGNED32   (A_APPLICATION | 2)     /* same as a guage */
#define VT_TIMETICKS    (A_APPLICATION | 3)
#define VT_OPAQUE       (A_APPLICATION | 4)
#define VT_COUNTER64    (A_APPLICATION | 6)
#define VT_UINTEGER32   (A_APPLICATION | 7)

/* values for exception data types */
#define VT_NOSUCHOBJ    (A_CONTEXT | 0)
#define VT_NOSUCHINS    (A_CONTEXT | 1)
#define VT_ENDOFMIB     (A_CONTEXT | 2)

/* Values for vb_flags  */
#define VFLAG_ALREADY_TEST      0x01
#define VFLAG_ALREADY_SET       0x02
#define VFLAG_ALREADY_GET       0x02
#define VFLAG_SA_SENDIT         0x04
#define VFLAG_SA_DOIT           0x08

/* more values for vb_flags some of them need to be alligned with the
   already_* flags, the rest are put in as we have space */

#define VFLAG_TEST_STARTED      0x10
#define VFLAG_TEST_DONE         0x01
#define VFLAG_SET_STARTED       0x20
#define VFLAG_SET_DONE          0x02
#define VFLAG_UNDO_STARTED      0x40
#define VFLAG_UNDO_DONE         0x80
#define VFLAG_UNDO_BOTH         (VFLAG_UNDO_STARTED | VFLAG_UNDO_DONE)

#define VFLAG_GET_STARTED       0x20
#define VFLAG_GET_DONE          0x02

#define VFLAG_NEXT_STARTED      0x20
#define VFLAG_NEXT_DONE         0x02
#define VFLAG_NEXT_REDO         0x10
#define VFLAG_NEXT_VCREQ        0x40
#define VFLAG_NEXT_VCDONE       0x80
#define VFLAG_NEXT_INSTANCE     0x01
#define VFLAG_NEXT_VCBITS       (VFLAG_NEXT_VCREQ | VFLAG_NEXT_VCDONE)
#define VFLAG_NEXT_FLAGS        0xF3

#define ENVOY_AX_FLAGS_INCLUDE     0x01
#define ENVOY_AX_FLAGS_CLEAN_LEAF  0x02
#define ENVOY_AX_FLAGS_CLEAN_INDEX 0x04

/* Define the shape of the VarBindList */
typedef struct VBL_S
{
    ALENGTH_T       vbl_length;     /* Length of the VarBindList seq */
    int             vbl_count;      /* Number of Var Bind items      */
    VB_T            *vblist;
    struct VBL_S    *vblp;          /* for get bulk chaining purposes */
} VBL_T;

#ifdef NEOSNET_STRUCT_ALIGN
#pragma align 0
#endif

/* macros to get the data from a vbp */
#define VB_GET_INT32(V)          ((V)->value_u.v_number)
#define VB_GET_UINT32(V)         ((V)->value_u.v_counter)
#define VB_GET_IP_ADDRESS(V)     ((V)->value_u.v_network_address)
#define VB_GET_STRING(V)        &((V)->value_u.v_string)
#define VB_GET_OBJECT_ID(V)     &((V)->value_u.v_object)
#define VB_GET_UINT64(V)        &((V)->value_u.v_counter64)

/* macro to get a pointer to a cookie, so that the user
   doesn't have to understand what a vb looks like */
#define VB_TO_COOKIE(vbp)       (vbp)->vb_ml.ml_leaf->user_cookie

/* various vb utility routines */
extern  VB_T *          VarBindList_Allocate __((int));
extern  VBL_T *         VBList_Allocate      __((int));
extern  ALENGTH_T       vbsize               __((VB_T *));
extern  int             Clean_vb_data        __((VB_T *));
extern  int             Clean_vb             __((VB_T *));
extern  void            Clean_vb_list        __((VBL_T *));
extern  void            VBList_Free          __((VBL_T *));

#ifdef __cplusplus
}
#endif

#endif /* (!defined(vb_inc)) */
